package leetcode.code0740;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int deleteAndEarn(int[] nums) {
//    	[2,2,3,3,3,4]
		Map<Integer, Integer> hash = new HashMap<>();
		for (int i = 0; i < nums.length; i++) {
			int num = nums[i];
			int sum = hash.getOrDefault(num, 0);
			hash.put(num, sum + num);
		}
		int[][] arr = new int[hash.size()][2];
		int p = 0;
		for (Map.Entry<Integer, Integer> entry : hash.entrySet()) {
			arr[p][0] = entry.getKey();
			arr[p][1] = entry.getValue();
			p++;
		}
		Arrays.sort(arr, (a, b) -> a[0] - b[0]);
		int[][] dp = new int[arr.length][2];
		dp[0][0] = 0;
		dp[0][1] = arr[0][1];
		for (int i = 1; i < arr.length; i++) {
			int last = arr[i - 1][0], cur = arr[i][0];
			dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]);
			dp[i][1] = cur - 1 > last ? dp[i][0] + arr[i][1] : dp[i - 1][0] + arr[i][1];
		}
		H.print(dp);
		return Math.max(dp[arr.length - 1][0], dp[arr.length - 1][1]);
	}

	@Override
	public void debug4() {
		H.compare(9, this.deleteAndEarn(H.str2array3("[2,2,3,3,3,4]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
